package com.yksj.healthtalk.ui.login;

import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.comm.SingleBtnFragmentDialog;
import com.yksj.healthtalk.net.http.AsyncHttpResponseHandler;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.JsonHttpResponseHandler;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.utils.ToastUtil;

/**
 * 找回密码
 * @author origin
 */
public class ResetPasswordUI extends BaseFragmentActivity implements OnClickListener {
	
	EditText mPhoneText;
	EditText mCodeText;
	Button mCodeButton;
	String valideCode;//验证码
	
	@SuppressLint("HandlerLeak")
	final Handler mHandler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			int time = (Integer)msg.obj;
			if(time == 0){
				mCodeButton.setText("获取");
				mCodeButton.setClickable(true);
			}else{
				mCodeButton.setText(time+"s");
				msg = mHandler.obtainMessage();
				msg.obj = --time;
				msg.what = 1;
				sendMessageDelayed(msg,1000);
			}
		};
	};
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.reset_password_layout);
		initUI();
	}
	
	private void initUI(){
		initTitle();
		
		setBackBtn("返回",this);
		
		setTitle("找回密码");
		
		mPhoneText = (EditText)findViewById(R.id.phone_txt);
		mCodeText = (EditText)findViewById(R.id.code_txt);
		mCodeButton = (Button)findViewById(R.id.get_code);
		
		findViewById(R.id.get_code).setOnClickListener(this);
		findViewById(R.id.login_btn).setOnClickListener(this);
		mCodeButton.setOnClickListener(this);
	}
	
	private void onSendCode(){
		if(mPhoneText.length() != 11){
			mPhoneText.setError("手机号码长度11位");
			return;
		}
		HttpRestClient.doHttpObtainVerificationCode(false,mPhoneText.getEditableText().toString(),new JsonHttpResponseHandler(this){
			@Override
			public void onSuccess(int statusCode, JSONObject response) {
				super.onSuccess(statusCode, response);
				if(response.has("error_code") && 0==response.optInt("error_code")){
					ToastUtil.showLong(ResetPasswordUI.this,response.optString("error_message"));
				}else{
					ToastUtil.showToastPanl(response.optString("message"));
					mCodeButton.setClickable(false);
					Message msg = mHandler.obtainMessage();
					msg.what = 1;
					msg.obj = 60;
					mHandler.sendMessageDelayed(msg,1000);
				}
			}
		});
		
		
	}
	
	private void onChangePassword(){
		if(mPhoneText.length() == 0){
			mPhoneText.setError("手机号不能为空");
			return;
		}
		//验证码
		if(mCodeText.length() == 0){
			mCodeText.setError("验证码不能为空");
			return;
		}
		HttpRestClient.doHttpResetPaswdOne(mPhoneText.getEditableText().toString(),
				mCodeText.getEditableText().toString(),
				new AsyncHttpResponseHandler(this){
				@Override
				public void onSuccess(int statusCode, String content) {
					if(!isFinishing()){
						try {
							JSONObject obt=new JSONObject(content);
							if("1".equals(obt.optString("error_code"))){
								SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(), obt.optString("验证成功"));
								Intent intent=new Intent(getApplicationContext(), ResetPasswordUItwo.class);
								intent.putExtra("CUSTOMER_ACCOUNTS", obt.optString("CUSTOMER_ACCOUNTS"));
								intent.putExtra("phone",obt.optString("phone"));
								intent.putExtra("code", obt.optString("code"));
								startActivity(intent);
								finish();
							}else{
								SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(), obt.optString("error_message"));
							}
						} catch (JSONException e) {
						}
					}
				}
		});
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.title_back:
			onBackPressed();
			break;
		case R.id.get_code:
			onSendCode();
			break;
		case R.id.login_btn:
			onChangePassword();
			break;
		}
	}
}
